{"componentChunkName":"component---src-templates-post-jsx","path":"/[BOJ]17144/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","excerpt":"문제 미세먼지를 제거하기 위해 구사과는 공기청정기를 설치하려고 한다. 공기청정기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기의 칸으로 나눴다. 구사과는 뛰어난 코딩 실력을 이용해 각 칸 (r, c)에 있는 미세먼지의 양을 실시간으로 모니터링하는 시스템을 개발했다. (r, c)는 r행 c열을 의미한다. 공기청정기…","html":"<h2>문제</h2>\n<p>미세먼지를 제거하기 위해 구사과는 공기청정기를 설치하려고 한다. 공기청정기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기의 칸으로 나눴다. 구사과는 뛰어난 코딩 실력을 이용해 각 칸 (r, c)에 있는 미세먼지의 양을 실시간으로 모니터링하는 시스템을 개발했다. (r, c)는 r행 c열을 의미한다.</p>\n<img src='https://upload.acmicpc.net/75d322ad-5a89-4301-b3a7-403fce0ff966/-/preview/' width='50%'>\n<p>공기청정기는 항상 1번 열에 설치되어 있고, 크기는 두 행을 차지한다. 공기청정기가 설치되어 있지 않은 칸에는 미세먼지가 있고, (r, c)에 있는 미세먼지의 양은 Ar,c이다.</p>\n<p>1초 동안 아래 적힌 일이 순서대로 일어난다.</p>\n<ol>\n<li>\n<p>미세먼지가 확산된다. 확산은 미세먼지가 있는 모든 칸에서 동시에 일어난다.</p>\n<ul>\n<li>(r, c)에 있는 미세먼지는 인접한 네 방향으로 확산된다.</li>\n<li>인접한 방향에 공기청정기가 있거나, 칸이 없으면 그 방향으로는 확산이 일어나지 않는다.</li>\n<li>확산되는 양은 Ar,c/5이고 소수점은 버린다.</li>\n<li>(r, c)에 남은 미세먼지의 양은 Ar,c - (Ar,c/5)×(확산된 방향의 개수) 이다.</li>\n</ul>\n</li>\n<li>\n<p>공기청정기가 작동한다.</p>\n<ul>\n<li>공기청정기에서는 바람이 나온다.</li>\n<li>위쪽 공기청정기의 바람은 반시계방향으로 순환하고, 아래쪽 공기청정기의 바람은 시계방향으로 순환한다.</li>\n<li>바람이 불면 미세먼지가 바람의 방향대로 모두 한 칸씩 이동한다.</li>\n<li>공기청정기에서 부는 바람은 미세먼지가 없는 바람이고, 공기청정기로 들어간 미세먼지는 모두 정화된다.</li>\n</ul>\n</li>\n</ol>\n<p>다음은 확산의 예시이다.</p>\n<img src='https://upload.acmicpc.net/7b0d9d57-1296-44cd-8951-4135d27f9446/-/preview/' width='30%'>\n<ul>\n<li>왼쪽과 오른쪽에 칸이 없기 때문에, 두 방향으로만 확산이 일어났다.</li>\n</ul>\n<img src='https://upload.acmicpc.net/cebebfa9-0056-45f1-b705-75b035888085/-/preview/' width='30%'>\n<ul>\n<li>인접한 네 방향으로 모두 확산이 일어난다.</li>\n</ul>\n<img src='https://upload.acmicpc.net/1ed0d2e9-9767-4b94-bbde-0e1d6a2d52ff/-/preview/' width='30%'>\n<ul>\n<li>공기청정기가 있는 칸으로는 확산이 일어나지 않는다.</li>\n</ul>\n<p>공기청정기의 바람은 다음과 같은 방향으로 순환한다.</p>\n<img src='https://upload.acmicpc.net/94466937-96c7-4f25-9804-530ebd554a59/-/preview/' width='50%'>\n<p>방의 정보가 주어졌을 때, T초가 지난 후 구사과의 방에 남아있는 미세먼지의 양을 구해보자.</p>\n<h2>입, 출력</h2>\n<p>입력</p>\n<ul>\n<li>첫째 줄에 R, C, T (6 ≤ R, C ≤ 50, 1 ≤ T ≤ 1,000) 가 주어진다.</li>\n<li>둘째 줄부터 R개의 줄에 Ar,c (-1 ≤ Ar,c ≤ 1,000)가 주어진다. 공기청정기가 설치된 곳은 Ar,c가 -1이고, 나머지 값은 미세먼지의 양이다. -1은 2번 위아래로 붙어져 있고, 가장 윗 행, 아랫 행과 두 칸이상 떨어져 있다.</li>\n</ul>\n<p>출력</p>\n<ul>\n<li>첫째 줄에 T초가 지난 후 구사과 방에 남아있는 미세먼지의 양을 출력한다.</li>\n</ul>\n<h3>📍 <strong>Logic</strong></h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Room {\n    Queue&lt;Pair&gt; dustQueue = new LinkedList&lt;&gt;();\n    int[][] room;\n    int[] rangeX = { -1, 0, 1, 0 };\n    int[] rangeY = { 0, 1, 0, -1 };\n    int R, C;\n    int cleaner1X, cleaner2X;\n    ...\n\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>미세 먼지와 공기청정기 정보를 저장하는 클래스</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">public int goodByeFineDust(int time) {\n    while(time &gt; 0) {\n        spreadDust();\n        runCleaner();\n        time--;\n    }\n    return countDust();\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>코드는 먼지를 확산시키고, 공기청정기를 동작시킨다. 정해진 시간이 경과한 후, 전체 미세 먼지를 반환한다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">private void spreadDust() {...}</code>\n        </deckgo-highlight-code>\n<ul>\n<li><code class=\"language-text\">Queue</code> 에 저장하고 있는 미세 먼지 좌표를 이용해 미세 먼지를 확산시킨다. </li>\n<li>이때 업데이트할 미세 먼지 정보를 <code class=\"language-text\">int[][] tmp</code> 에 저장하고, 확산이 끝난 이후 <code class=\"language-text\">room</code> 을 업데이트한다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">private void runCleaner() {...}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>공기 청정기의 바람 방향을 따라 순차적으로 미세 먼지를 이동시킨다. </li>\n<li>공기 청정기 바로 앞 좌표는 항상 <code class=\"language-text\">0</code> 으로 업데이트한다.</li>\n</ul>\n<h3>📄 <strong>CODE</strong></h3>\n<details>\n  <summary>코드 보기/접기💫</summary>\n    <div markdown=\"1\">\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.util.Arrays;\nimport java.util.LinkedList;\nimport java.util.Queue;\n\nclass Pair {\n    int x;\n    int y;\n\n    public Pair(int x, int y) {\n        this.x = x;\n        this.y = y;\n    }\n}\n\nclass Room {\n    Queue&lt;Pair&gt; dustQueue = new LinkedList&lt;&gt;();\n    int[][] room;\n    int[] rangeX = { -1, 0, 1, 0 };\n    int[] rangeY = { 0, 1, 0, -1 };\n    int R, C;\n    int cleaner1X, cleaner2X;\n\n    public Room (int R, int C) {\n        this.R = R;\n        this.C = C;\n        room = new int[R][C];\n    }\n\n    public int goodByeFineDust(int time) {\n        while(time &gt; 0) {\n            spreadDust();\n            runCleaner();\n            time--;\n        }\n        return countDust();\n    }\n\n    private void spreadDust() {\n        int[][] tmp = new int[R][C];\n\n        while(!dustQueue.isEmpty()) {\n            Pair dust = dustQueue.poll();\n            int divDust = room[dust.x][dust.y] / 5;\n            int checkSpread = 0;\n\n            for (int i = 0; i &lt; 4; i++) {\n                int nx = dust.x + rangeX[i];\n                int ny = dust.y + rangeY[i];\n\n                if (nx &lt; 0 || nx &gt; R - 1 || ny &lt; 0 || ny &gt; C - 1) continue;\n                if (room[nx][ny] == -1) continue;\n\n                tmp[nx][ny] += divDust;\n                checkSpread++;\n            }\n            tmp[dust.x][dust.y] = tmp[dust.x][dust.y] + room[dust.x][dust.y] - (divDust * checkSpread);\n        }\n\n        for (int i = 0; i &lt; R; i++) {\n            for (int j = 0; j &lt; C; j++) {\n                if (room[i][j] == -1) continue;\n                room[i][j] = tmp[i][j];\n            }\n        }\n    }\n\n    private void runCleaner() {\n        int i = 0;\n        for (i = cleaner1X - 2; i &gt;= 0; i--) {\n            room[i + 1][0] = room[i][0];\n        }\n        for (i = 1; i &lt; C; i++) {\n            room[0][i - 1] = room[0][i];\n        }\n        for (i = 1; i &lt;= cleaner1X; i++) {\n            room[i - 1][C - 1] = room[i][C - 1];\n        }\n        for (i = C - 2; i &gt; 0; i--) {\n            room[cleaner1X][i + 1] = room[cleaner1X][i];\n        }\n\n        for (i = cleaner2X + 2; i &lt; R; i++) {\n            room[i - 1][0] = room[i][0];\n        }\n        for (i = 1; i &lt; C; i++) {\n            room[R - 1][i - 1] = room[R - 1][i];\n        }\n        for (i = R - 2; i &gt;= cleaner2X; i--) {\n            room[i + 1][C - 1] = room[i][C - 1];\n        }\n        for (i = C - 2; i &gt; 0; i--) {\n            room[cleaner2X][i + 1] = room[cleaner2X][i];\n        }\n\n        room[cleaner1X][1] = room[cleaner2X][1] = 0;\n\n        for (i = 0; i &lt; R; i++) {\n            for (int j = 0; j &lt; C; j++) {\n                if (room[i][j] &gt; 0) dustQueue.add(new Pair(i, j));\n            }\n        }\n    }\n\n    private int countDust() {\n        int count = 0;\n\n        for (int i = 0; i &lt; R; i++) {\n            for (int j = 0; j &lt; C; j++) {\n                if (room[i][j] &gt; 0) count += room[i][j];\n            }\n        }\n        return count;\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) throws IOException {\n        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n\n        int[] line = Arrays.stream(br.readLine().split(&quot; &quot;)).mapToInt(Integer::parseInt).toArray();\n        int R = line[0], C = line[1], T = line[2];\n\n        Room dirtyRoom = new Room(R, C);\n\n        for (int i = 0; i &lt; R; i++) {\n            dirtyRoom.room[i] = Arrays.stream(br.readLine().split(&quot; &quot;)).mapToInt(Integer::parseInt).toArray();\n            if (dirtyRoom.room[i][0] == -1 &amp;&amp; dirtyRoom.cleaner1X == 0) {\n                dirtyRoom.cleaner1X = i;\n                dirtyRoom.cleaner2X = i+1;\n            }\n            for (int j = 0; j &lt; C; j++) {\n                if (dirtyRoom.room[i][j] &gt; 0) dirtyRoom.dustQueue.add(new Pair(i, j));\n            }\n        }\n        br.close();\n\n        System.out.println(dirtyRoom.goodByeFineDust(T));\n    }\n}</code>\n        </deckgo-highlight-code>\n  \t</div>\n</details>\n<h3>✏️ <strong>Review</strong></h3>\n<ul>\n<li>미세먼지를 확산시키는 방법에서 시간을 많이 뺏겼다. 결론적으로는 추가적인 배열을 두고, 미세먼지 확산 정보를 저장해두고, <code class=\"language-text\">room</code> 에 업데이트하는 방식이 필요했다.</li>\n<li>시뮬레이션은 단순한 듯 하면서 많은 코드량을 요구하는 것 같다...</li>\n</ul>\n<h3>📕 출처</h3>\n<p>Baekjoon : <a href=\"https://www.acmicpc.net/problem/17144\">https://www.acmicpc.net/problem/17144</a></p>","frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!","date":"April 01, 2022","update":"April 01, 2022","tags":["구현","시뮬레이션"],"series":"👩‍💻 BOJ"},"fields":{"slug":"/[BOJ]17144/","readingTime":{"minutes":8.445}}},"seriesList":{"edges":[{"node":{"id":"27733333-3aad-5771-a1a1-27b4de4b4a01","fields":{"slug":"/[BOJ]2225/"},"frontmatter":{"title":"👩‍💻 2225. 합분해"}}},{"node":{"id":"548ba188-2afe-5575-b79f-107c6c65b07d","fields":{"slug":"/[BOJ]1012/"},"frontmatter":{"title":"👩‍💻 1012. 유기농 배추"}}},{"node":{"id":"0c712680-130f-5fa5-8c3b-3632dd5d1af0","fields":{"slug":"/[BOJ]1260/"},"frontmatter":{"title":"👩‍💻 1260. DFS와 BFS"}}},{"node":{"id":"0e07a592-06ce-5e01-b5e1-95398330372e","fields":{"slug":"/[BOJ]1707/"},"frontmatter":{"title":"👩‍💻 1707. 이분그래프"}}},{"node":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","fields":{"slug":"/[BOJ]2668/"},"frontmatter":{"title":"👩‍💻 2668. 숫자 고르기"}}},{"node":{"id":"01bbb6dc-812c-5223-87c5-53bf04c0d0a6","fields":{"slug":"/[BOJ]9466/"},"frontmatter":{"title":"👩‍💻 9466. 텀 프로젝트"}}},{"node":{"id":"abc306b3-c3a6-552a-8979-e82d506b3921","fields":{"slug":"/[BOJ]2146/"},"frontmatter":{"title":"👩‍💻 2146. 다리 만들기"}}},{"node":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","fields":{"slug":"/[BOJ]5014/"},"frontmatter":{"title":"👩‍💻 5014. 스타트 링크"}}},{"node":{"id":"be297703-e778-5727-8019-6954bc95da3a","fields":{"slug":"/[BOJ]11559/"},"frontmatter":{"title":"👩‍💻 11559. Puyo Puyo"}}},{"node":{"id":"ddf11545-d468-5269-94b1-5daa2720e886","fields":{"slug":"/[BOJ]17471/"},"frontmatter":{"title":"👩‍💻 17471. 게리맨더링"}}},{"node":{"id":"8d77114d-fc6e-5de2-a3ca-c3b250425a56","fields":{"slug":"/[BOJ]2437/"},"frontmatter":{"title":"👩‍💻 2437. 저울"}}},{"node":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","fields":{"slug":"/[BOJ]14891/"},"frontmatter":{"title":"👩‍💻 14891. 톱니바퀴"}}},{"node":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","fields":{"slug":"/[BOJ]14503/"},"frontmatter":{"title":"👩‍💻 14503. 로봇 청소기"}}},{"node":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","fields":{"slug":"/[BOJ]3190/"},"frontmatter":{"title":"👩‍💻 3190. 뱀"}}},{"node":{"id":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a","fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},{"node":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","fields":{"slug":"/[BOJ]17779/"},"frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2"}}},{"node":{"id":"aa67d90b-4801-574c-b225-56c23737a5e2","fields":{"slug":"/[BOJ]2579/"},"frontmatter":{"title":"👩‍💻 2579. 계단 오르기"}}},{"node":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","fields":{"slug":"/[BOJ]14499/"},"frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기"}}},{"node":{"id":"6a79cddd-dc1b-5e34-8a84-af313c37de0e","fields":{"slug":"/[BOJ]15685/"},"frontmatter":{"title":"👩‍💻 15685. 드래곤 커브"}}},{"node":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","fields":{"slug":"/[BOJ]17144/"},"frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!"}}},{"node":{"id":"1d132410-9377-5e55-85b9-4664ff64de73","fields":{"slug":"/[BOJ]9935/"},"frontmatter":{"title":"👩‍💻 9935. 문자열 폭발"}}},{"node":{"id":"e13ff8d6-9b02-56c0-83e2-1d3144f70e1d","fields":{"slug":"/[BOJ]16472/"},"frontmatter":{"title":"👩‍💻 16472. 고냥이"}}},{"node":{"id":"0f782160-aaa9-5c54-80c0-72a15172dd16","fields":{"slug":"/[BOJ]1083/"},"frontmatter":{"title":"👩‍💻 1083. 소트"}}},{"node":{"id":"fef58615-9e18-5f50-8d09-3d1e2839342f","fields":{"slug":"/[BOJ]20922/"},"frontmatter":{"title":"👩‍💻 20922. 겹치는 건 싫어"}}},{"node":{"id":"7ca89240-c65f-5cb4-aeef-c5f1605ebd00","fields":{"slug":"/[BOJ]16172/"},"frontmatter":{"title":"👩‍💻 16172. 나는 친구가 적다(Large)"}}}]},"previous":{"fields":{"slug":"/[JPA]11/"},"frontmatter":{"title":"📚 11. 웹 애플리케이션 제작"}},"next":{"fields":{"slug":"/[JPA]12/"},"frontmatter":{"title":"📚 12. 스프링 데이터 JPA"}}},"pageContext":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","series":"👩‍💻 BOJ","previousPostId":"4ef1a134-ac1d-5e69-9f85-7c1b5d7a8244","nextPostId":"1a74ae89-3758-5cb9-938c-b8238d176c8c"}},"staticQueryHashes":[]}